<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>华容道游戏</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
            color: white;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            align-items: center;
            padding: 20px;
        }
        
        .container {
            max-width: 800px;
            width: 100%;
            text-align: center;
        }
        
        header {
            margin-bottom: 30px;
        }
        
        h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
        }
        
        .game-info {
            display: flex;
            justify-content: space-between;
            margin-bottom: 20px;
            background: rgba(255, 255, 255, 0.1);
            padding: 15px;
            border-radius: 10px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
        }
        
        .moves, .timer {
            font-size: 1.2rem;
        }
        
        .game-board {
            width: 400px;
            height: 500px;
            margin: 0 auto 30px;
            background: #1a1a2e;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
            position: relative;
        }
        
        .board {
            width: 360px;
            height: 460px;
            background: #16213e;
            border-radius: 5px;
            position: relative;
            margin: 0 auto;
            overflow: hidden;
            border: 2px solid #0f3460;
        }
        
        .exit {
            position: absolute;
            bottom: 0;
            right: 80px;
            width: 80px;
            height: 20px;
            background: #e94560;
            z-index: 10;
        }
        
        .piece {
            position: absolute;
            background: #e94560;
            border-radius: 5px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-weight: bold;
            cursor: pointer;
            user-select: none;
            transition: transform 0.2s, box-shadow 0.2s;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
        }
        
        .piece:hover {
            transform: scale(1.02);
            box-shadow: 0 6px 12px rgba(0, 0, 0, 0.4);
        }
        
        .piece.caocao {
            background: linear-gradient(135deg, #ff416c, #ff4b2b);
            font-size: 1.2rem;
        }
        
        .piece.vertical {
            background: linear-gradient(135deg, #2193b0, #6dd5ed);
        }
        
        .piece.horizontal {
            background: linear-gradient(135deg, #834d9b, #d04ed6);
        }
        
        .piece.soldier {
            background: linear-gradient(135deg, #00b09b, #96c93d);
        }
        
        .controls {
            display: flex;
            gap: 15px;
            justify-content: center;
            margin-bottom: 20px;
        }
        
        button {
            padding: 12px 25px;
            font-size: 1rem;
            background: #e94560;
            color: white;
            border: none;
            border-radius: 50px;
            cursor: pointer;
            transition: all 0.3s;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
        }
        
        button:hover {
            background: #ff6b81;
            transform: translateY(-2px);
            box-shadow: 0 6px 12px rgba(0, 0, 0, 0.3);
        }
        
        button:active {
            transform: translateY(1px);
        }
        
        .instructions {
            background: rgba(255, 255, 255, 0.1);
            padding: 20px;
            border-radius: 10px;
            margin-top: 20px;
            text-align: left;
        }
        
        .instructions h2 {
            margin-bottom: 10px;
            text-align: center;
        }
        
        .instructions ul {
            list-style-type: none;
            padding-left: 20px;
        }
        
        .instructions li {
            margin-bottom: 8px;
            position: relative;
            padding-left: 20px;
        }
        
        .instructions li:before {
            content: "•";
            color: #e94560;
            font-weight: bold;
            position: absolute;
            left: 0;
        }
        
        .win-message {
            display: none;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: rgba(0, 0, 0, 0.8);
            padding: 30px;
            border-radius: 10px;
            text-align: center;
            z-index: 100;
            box-shadow: 0 0 30px rgba(233, 69, 96, 0.7);
        }
        
        .win-message h2 {
            font-size: 2rem;
            margin-bottom: 20px;
            color: #e94560;
        }
        
        @media (max-width: 500px) {
            .game-board {
                width: 320px;
                height: 400px;
            }
            
            .board {
                width: 280px;
                height: 360px;
            }
            
            .piece {
                font-size: 0.8rem;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>华容道</h1>
            <p>移动方块，让红色方块（曹操）从底部出口逃脱</p>
        </header>
        
        <div class="game-info">
            <div class="moves">移动步数: <span id="moveCount">0</span></div>
            <div class="timer">用时: <span id="timer">00:00</span></div>
        </div>
        
        <div class="game-board">
            <div class="board" id="board">
                <div class="exit"></div>
                <!-- 方块将通过JavaScript动态生成 -->
            </div>
            <div class="win-message" id="winMessage">
                <h2>恭喜过关！</h2>
                <p>移动步数: <span id="finalMoves">0</span></p>
                <p>用时: <span id="finalTime">00:00</span></p>
                <button id="playAgain">再玩一次</button>
            </div>
        </div>
        
        <div class="controls">
            <button id="resetBtn">重新开始</button>
            <button id="hintBtn">提示</button>
        </div>
        
        <div class="instructions">
            <h2>游戏规则</h2>
            <ul>
                <li>拖动方块，让最大的红色方块（曹操）移动到棋盘底部的出口</li>
                <li>只能水平或垂直移动方块，不能旋转</li>
                <li>所有方块都不能移出棋盘边界</li>
                <li>用最少的步数和最短的时间完成挑战</li>
            </ul>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 游戏配置
            const config = {
                boardWidth: 360,
                boardHeight: 460,
                cellSize: 80,
                gap: 10
            };
            
            // 游戏状态
            let gameState = {
                pieces: [],
                moveCount: 0,
                startTime: null,
                timerInterval: null,
                isGameOver: false
            };
            
            // 初始化游戏
            function initGame() {
                const board = document.getElementById('board');
                board.innerHTML = '<div class="exit"></div>';
                
                // 重置游戏状态
                gameState.pieces = [];
                gameState.moveCount = 0;
                gameState.isGameOver = false;
                document.getElementById('moveCount').textContent = '0';
                document.getElementById('winMessage').style.display = 'none';
                
                // 停止之前的计时器
                if (gameState.timerInterval) {
                    clearInterval(gameState.timerInterval);
                }
                
                // 开始计时
                gameState.startTime = new Date();
                gameState.timerInterval = setInterval(updateTimer, 1000);
                updateTimer();
                
                // 创建方块
                createPieces();
                
                // 渲染方块
                renderPieces();
            }
            
            // 创建方块
            function createPieces() {
                // 曹操 (2x2)
                gameState.pieces.push({
                    id: 'caocao',
                    name: '曹操',
                    width: 2,
                    height: 2,
                    x: 1,
                    y: 0,
                    type: 'caocao'
                });
                
                // 关羽 (2x1)
                gameState.pieces.push({
                    id: 'guanyu',
                    name: '关羽',
                    width: 2,
                    height: 1,
                    x: 1,
                    y: 2,
                    type: 'horizontal'
                });
                
                // 张飞 (1x2)
                gameState.pieces.push({
                    id: 'zhangfei',
                    name: '张飞',
                    width: 1,
                    height: 2,
                    x: 0,
                    y: 0,
                    type: 'vertical'
                });
                
                // 赵云 (1x2)
                gameState.pieces.push({
                    id: 'zhaoyun',
                    name: '赵云',
                    width: 1,
                    height: 2,
                    x: 3,
                    y: 0,
                    type: 'vertical'
                });
                
                // 马超 (1x2)
                gameState.pieces.push({
                    id: 'machao',
                    name: '马超',
                    width: 1,
                    height: 2,
                    x: 0,
                    y: 2,
                    type: 'vertical'
                });
                
                // 黄忠 (1x2)
                gameState.pieces.push({
                    id: 'huangzhong',
                    name: '黄忠',
                    width: 1,
                    height: 2,
                    x: 3,
                    y: 2,
                    type: 'vertical'
                });
                
                // 士兵 (1x1)
                gameState.pieces.push({
                    id: 'soldier1',
                    name: '士兵',
                    width: 1,
                    height: 1,
                    x: 1,
                    y: 4,
                    type: 'soldier'
                });
                
                gameState.pieces.push({
                    id: 'soldier2',
                    name: '士兵',
                    width: 1,
                    height: 1,
                    x: 2,
                    y: 3,
                    type: 'soldier'
                });
                
                gameState.pieces.push({
                    id: 'soldier3',
                    name: '士兵',
                    width: 1,
                    height: 1,
                    x: 1,
                    y: 3,
                    type: 'soldier'
                });
                
                gameState.pieces.push({
                    id: 'soldier4',
                    name: '士兵',
                    width: 1,
                    height: 1,
                    x: 2,
                    y: 4,
                    type: 'soldier'
                });
            }
            
            // 渲染方块
            function renderPieces() {
                const board = document.getElementById('board');
                
                gameState.pieces.forEach(piece => {
                    const pieceElement = document.createElement('div');
                    pieceElement.className = `piece ${piece.type}`;
                    pieceElement.id = piece.id;
                    pieceElement.textContent = piece.name;
                    
                    // 计算位置和尺寸
                    const left = piece.x * config.cellSize + (piece.x + 1) * config.gap;
                    const top = piece.y * config.cellSize + (piece.y + 1) * config.gap;
                    const width = piece.width * config.cellSize + (piece.width - 1) * config.gap;
                    const height = piece.height * config.cellSize + (piece.height - 1) * config.gap;
                    
                    pieceElement.style.width = `${width}px`;
                    pieceElement.style.height = `${height}px`;
                    pieceElement.style.left = `${left}px`;
                    pieceElement.style.top = `${top}px`;
                    
                    // 添加拖拽事件
                    makeDraggable(pieceElement, piece);
                    
                    board.appendChild(pieceElement);
                });
            }
            
            // 使方块可拖拽
            function makeDraggable(element, piece) {
                let isDragging = false;
                let startX, startY, initialX, initialY;
                
                element.addEventListener('mousedown', startDrag);
                element.addEventListener('touchstart', startDrag);
                
                function startDrag(e) {
                    if (gameState.isGameOver) return;
                    
                    e.preventDefault();
                    isDragging = true;
                    
                    // 获取初始位置
                    initialX = piece.x;
                    initialY = piece.y;
                    
                    // 获取鼠标/触摸起始位置
                    if (e.type === 'mousedown') {
                        startX = e.clientX;
                        startY = e.clientY;
                        document.addEventListener('mousemove', drag);
                        document.addEventListener('mouseup', stopDrag);
                    } else {
                        startX = e.touches[0].clientX;
                        startY = e.touches[0].clientY;
                        document.addEventListener('touchmove', drag);
                        document.addEventListener('touchend', stopDrag);
                    }
                    
                    // 提高被拖拽元素的z-index
                    element.style.zIndex = '100';
                }
                
                function drag(e) {
                    if (!isDragging) return;
                    
                    e.preventDefault();
                    let currentX, currentY;
                    
                    if (e.type === 'mousemove') {
                        currentX = e.clientX;
                        currentY = e.clientY;
                    } else {
                        currentX = e.touches[0].clientX;
                        currentY = e.touches[0].clientY;
                    }
                    
                    // 计算移动距离（以格子为单位）
                    const deltaX = currentX - startX;
                    const deltaY = currentY - startY;
                    
                    // 确定移动方向（水平或垂直）
                    const absDeltaX = Math.abs(deltaX);
                    const absDeltaY = Math.abs(deltaY);
                    
                    if (absDeltaX > absDeltaY && absDeltaX > 10) {
                        // 水平移动
                        const direction = deltaX > 0 ? 1 : -1;
                        tryMove(piece, direction, 0);
                    } else if (absDeltaY > absDeltaX && absDeltaY > 10) {
                        // 垂直移动
                        const direction = deltaY > 0 ? 1 : -1;
                        tryMove(piece, 0, direction);
                    }
                }
                
                function stopDrag() {
                    if (!isDragging) return;
                    
                    isDragging = false;
                    document.removeEventListener('mousemove', drag);
                    document.removeEventListener('mouseup', stopDrag);
                    document.removeEventListener('touchmove', drag);
                    document.removeEventListener('touchend', stopDrag);
                    
                    // 重置z-index
                    element.style.zIndex = '';
                    
                    // 检查是否胜利
                    checkWin();
                }
            }
            
            // 尝试移动方块
            function tryMove(piece, dx, dy) {
                if (dx === 0 && dy === 0) return false;
                
                // 计算新位置
                const newX = piece.x + dx;
                const newY = piece.y + dy;
                
                // 检查边界
                if (newX < 0 || newY < 0 || 
                    newX + piece.width > 4 || 
                    newY + piece.height > 5) {
                    return false;
                }
                
                // 检查与其他方块的碰撞
                for (const otherPiece of gameState.pieces) {
                    if (otherPiece.id === piece.id) continue;
                    
                    if (newX < otherPiece.x + otherPiece.width &&
                        newX + piece.width > otherPiece.x &&
                        newY < otherPiece.y + otherPiece.height &&
                        newY + piece.height > otherPiece.y) {
                        return false;
                    }
                }
                
                // 移动方块
                piece.x = newX;
                piece.y = newY;
                
                // 更新UI
                updatePiecePosition(piece);
                
                // 增加移动计数
                if (dx !== 0 || dy !== 0) {
                    gameState.moveCount++;
                    document.getElementById('moveCount').textContent = gameState.moveCount;
                }
                
                return true;
            }
            
            // 更新方块位置
            function updatePiecePosition(piece) {
                const element = document.getElementById(piece.id);
                const left = piece.x * config.cellSize + (piece.x + 1) * config.gap;
                const top = piece.y * config.cellSize + (piece.y + 1) * config.gap;
                
                element.style.left = `${left}px`;
                element.style.top = `${top}px`;
            }
            
            // 检查是否胜利
            function checkWin() {
                const caocao = gameState.pieces.find(p => p.id === 'caocao');
                
                // 检查曹操是否到达出口位置（底部中间）
                if (caocao.x === 1 && caocao.y === 3) {
                    gameState.isGameOver = true;
                    clearInterval(gameState.timerInterval);
                    
                    // 显示胜利消息
                    const winMessage = document.getElementById('winMessage');
                    document.getElementById('finalMoves').textContent = gameState.moveCount;
                    document.getElementById('finalTime').textContent = document.getElementById('timer').textContent;
                    winMessage.style.display = 'block';
                }
            }
            
            // 更新计时器
            function updateTimer() {
                const currentTime = new Date();
                const elapsedTime = Math.floor((currentTime - gameState.startTime) / 1000);
                const minutes = Math.floor(elapsedTime / 60).toString().padStart(2, '0');
                const seconds = (elapsedTime % 60).toString().padStart(2, '0');
                document.getElementById('timer').textContent = `${minutes}:${seconds}`;
            }
            
            // 提示功能
            function showHint() {
                alert('提示：先移动士兵，为曹操腾出空间，然后水平移动曹操到出口位置。');
            }
            
            // 事件监听
            document.getElementById('resetBtn').addEventListener('click', initGame);
            document.getElementById('hintBtn').addEventListener('click', showHint);
            document.getElementById('playAgain').addEventListener('click', initGame);
            
            // 初始化游戏
            initGame();
        });
    </script>
</body>
</html>